package mylibrary.network.socket;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class ExecutionThread implements Runnable {

	private OutputStream os;
	private Socket socket;
	public ExecutionThread(Socket socket, OutputStream os) {
		this.socket = socket;
		this.os  = os;
	}
	
	
	public void run() {
		
		try {
			//action costing much time
			InputStream in = new FileInputStream("E:\\Installation Files\\ahmedyha-LightCouch-lightcouch-0.0.4-20-g9f174b2.zip");
			MessageDigest sha = MessageDigest.getInstance("SHA");
			DigestInputStream din = new DigestInputStream(in, sha);

			int b = 0;
			while((b=in.read()) != -1) ;
			din.close();
			
			byte[] digest = sha.digest();
			StringBuffer result = new StringBuffer("E:\\Installation Files\\ahmedyha-LightCouch-lightcouch-0.0.4-20-g9f174b2.zip");
			result.append(": ");
			
			for(int k=0; k<digest.length; k++) {
				result.append(digest[k] + " ");
			}	
			
			OutputStreamWriter writer = new OutputStreamWriter(os);
			writer.write("nihao");
			writer.flush();
			//action costing much time
			
			
			System.out.println("finished");
			
			socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
